﻿<%@ Page Title="" Language="C#" MasterPageFile="~/manager/page/ManagerMasterPage.Master" AutoEventWireup="true" CodeBehind="storelist.aspx.cs" Inherits="Com.Web.coffinegurunaru.manager.store.storelist" %>
<%@ Register TagPrefix="UC" TagName="Menu" Src="~/manager/store/menu.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="phHead" runat="server">
<script type="text/javascript" src="http://openapi.map.naver.com/js/naverMap.naver?key=<%=NaverAPI.GetAPIKey()%>"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="phContent" runat="server">
<hr />
<br />
<table width="100%" border="0">
<tr>
	<td valign="top" width="60">
<UC:Menu ID="Menu1" SubPageType=2 runat="server" />
	</td>
	<td style="width:20px;"></td>
	<td valign="top">

<table cellpadding="0" cellspacing="1" style="background-color:Gray; width:100%;">
<tr>
	<td style="background-color:Silver; text-align:center; width:150px; height:30px;">가맹점 상태</td>
	<td style="background-color:#ffffff; ">&nbsp;&nbsp;
		<select id="m_selIsActive" style="width:200px">
			<option value="Y">활성</option>
			<option value="N">비활성</option>
		</select>
	</td>
</tr>
<tr>
	<td style="background-color:Silver; text-align:center; width:150px; height:30px;">지역</td>
	<td style="background-color:#ffffff; ">&nbsp;&nbsp;
		<select id="m_selArea" style="width:200px">
			<option value="" >전체</option>
			<option value="강원" >강원</option>
			<option value="경기" >경기</option>
			<option value="경남" >경남</option>
			<option value="경북" >경북</option>
			<option value="광주" >광주</option>
			<option value="대구" >대구</option>
			<option value="대전" >대전</option>
			<option value="부산" >부산</option>
			<option value="서울" >서울</option>
			<option value="울산" >울산</option>
			<option value="인천" >인천</option>
			<option value="전남" >전남</option>
			<option value="전북" >전북</option>
			<option value="제주" >제주</option>
			<option value="충남" >충남</option>
			<option value="충북" >충북</option>
		</select>
	</td>
</tr>
<tr>
	<td style="background-color:Silver; text-align:center; width:150px; height:30px;">가맹점 이름</td>
	<td style="background-color:#ffffff; ">&nbsp;&nbsp;
		<input type="text" id="m_StoreName" />&nbsp;<input type="button" id="m_StoreNameSearch" value="검색" />
	</td>
</tr>
</table>
<br />
<div id="m_divStoreList">
<table cellpadding="0" cellspacing="1" style="background-color:Gray; width:100%;" id="m_StoreList">
<tr style="background-color:Silver; text-align:center; height:30px;">
	<td style="width:120px;">아이디</td>
	<td style="width:120px;">가맹점명</td>
	<td style="width:150px;">전화번호</td>
	<td style="width:100px;">지역</td>
	<td style="width:250px;">주소</td>
	<td style="width:120px;">등록일</td>
	<td style="width:120px;">등록자</td>
	<td style="width:80px;"> - </td>
</tr>
</table>
<br />
<table width="100%">
<tr>
	<td align="center">
<div id="m_Pager"></div>
	</td>
</tr>
</table>
</div>
<table>
<tr><td style="width:500px;" valign="top">
<div id="m_StoreModify"></div>
</td>
<td style="width:20px;"></td>
<td valign="top">
<div id="m_NaverAPI" style=" display:none;">
	<div id="m_NaverAddrList" style="width:520px; height:200px; overflow:scroll;" ></div><br />
	<div id="m_MapContainer" style="width:600px; height:400px;"></div>
</div>
</td>
</tr>
</table>
	</td>
</tr>
</table>

<script language="javascript">
(function($){
	$.StoreList = {};
	var StoreName = "";
	$.StoreList = {
		Init : function() {
			$("#m_Pager").pager({
				pageSize : 20,
				useCallback : true,
				pageNo : 1,
				callback:function(pageNo) {
					$.StoreList.GetList();
				}
			});
			
			$("#m_selIsActive").change(function() {
				$("#m_Pager").pager("option", "currentPageNo", 1);
				$.StoreList.GetList();
			});
			$("#m_selArea").change(function() {
				$("#m_Pager").pager("option", "currentPageNo", 1);
				$.StoreList.GetList();
			});
			$("#m_StoreNameSearch").click(function() {
				StoreName = $("#m_StoreName").val();
				$("#m_Pager").pager("option", "currentPageNo", 1);
				$.StoreList.GetList();
			});
			
			$("#m_StoreName").keypress(function(e) {
				if ( e.keyCode == 13 ) {
					$("#m_StoreNameSearch").click();
					return false;
				}
			});
			
			$.StoreList.GetList();
		},
		
		GetList : function() {
			if ( $("#m_selIsActive").val() == "N" ) {
				if ( $("#m_selArea").val() != "" || $("#m_StoreName").val() != "" ) {
					alert( "비활성된 가맹점은 검색할수 없습니다.");
					return false;
				}
			}
			$("#m_StoreList").ajaxNet(
			"/manager/store/storemethod.aspx?_vb=GetStoreList",
			{
				"IsActive": $("#m_selIsActive").val()
			,	"CityName" : $("#m_selArea").val()
			,	"StoreName" : StoreName
			,	"PageNo": $("#m_Pager").pager("option", "currentPageNo")
			,	"PageSize": $("#m_Pager").pager("option", "pageSize")
			}, function(results, context) {
				$("#m_StoreList tr:gt(0)").remove();
				$("#m_StoreList tbody").append( results[0] );
				$("#m_Pager").pager("update", results[1]);
				$("#m_StoreList input.m_btnModify").click(function() {
					var $tr = $(this).closest("tr");
					var data = $tr.tagdata();
					$.StoreList.GetInfo( data.StoreNo );
				});
			});
		},
		
		Callback : function( url )
		{
			$("#m_StoreModify>table.m_UpdateStore td.m_FileList table tr").append(
				$("<td align='center'><img src='" + url + "' width='80' height='80' /><input type='hidden' class='m_ImageURL' value='" + url + "'/><br/><a href='#' onclick='$.StoreList.ImageRemove(this); return false;'>삭제</a></td>")
			);
		},
		
		ImageRemove : function( elem )
		{
			if ( confirm("이미지를 삭제하시겠습니까?" ) )
				$(elem).closest("td").remove();
		},
		
		SetPost : function(postno1, postno2, address )
		{
			$("#m_StoreModify>table.m_UpdateStore input.m_PostNo1").val(postno1);
			$("#m_StoreModify>table.m_UpdateStore input.m_PostNo2").val(postno2);
			$("#m_StoreModify>table.m_UpdateStore input.m_Address").val(address).focus().select();
			
			$("#m_StoreModify>table.m_UpdateStore input.m_PointX").val( "" );
			$("#m_StoreModify>table.m_UpdateStore input.m_PointY").val( "" );
			
		},
		
		GetInfo : function(storeNo)
		{
			$("#m_StoreModify").ajaxNet(
			"/manager/store/storemethod.aspx?_vb=GetStoreInfo",
			{
				"StoreNo": storeNo
			}, function(results, context) {
			
				$("#m_StoreModify").html(results);
				$("#m_divStoreList").hide();
				$("#m_NaverAPI").hide();
				
				$("#m_StoreModify>table.m_UpdateStore input.m_btnPostSearch").click(function() {
					$.manager.postopen("$.StoreList.SetPost");
				});
				
				$("#m_btnStoreModify").click(function() {
					$.StoreList.Modify();
				});
				$("#m_btnStoreCancel").click(function() {
					$("#m_divStoreList").show();
					$("#m_StoreModify").html("");
					$("#m_NaverAPI").hide();
				});
				
				$("#m_StoreModify>table.m_UpdateStore select.m_IsTakeout").change(function() {
					if ( $(this).val() == "3" )
						$("#m_StoreModify>table.m_UpdateStore input.m_Takeout").show()
					else
						$("#m_StoreModify>table.m_UpdateStore input.m_Takeout").hide()
				});
				
				$("#m_StoreModify>table.m_UpdateStore select.m_IsParking").change(function() {
					if ( $(this).val() == "3" )
						$("#m_StoreModify>table.m_UpdateStore input.m_Parking").show()
					else
						$("#m_StoreModify>table.m_UpdateStore input.m_Parking").hide()
				});
				
				$("#m_StoreModify>table.m_UpdateStore input.m_NaverMapSearch").click(function() {
					var pointX = $("#m_StoreModify>table.m_UpdateStore input.m_PointX").val();
					var pointY = $("#m_StoreModify>table.m_UpdateStore input.m_PointY").val();
					var address = $("#m_StoreModify>table.m_UpdateStore input.m_Address").val();
					$("#m_NaverAPI").show();
					
					
					$("#m_NaverAddrList").ajaxNet(
					"/manager/store/storemethod.aspx?_vb=GetNaverAddr",
					{
						"Address": address
					}, function(results, context) {
						$("#m_NaverAddrList").html(results);
						$("#m_NaverAddrList").find("tr").click(function() {
							var $tr = $(this);
							var address = $tr.find("td:eq(0)").text();
							var pointX = $tr.find("td:eq(1)").text();
							var pointY = $tr.find("td:eq(2)").text();
							$.StoreList.NaverMapViewr(pointX, pointY, address);
							$("#m_StoreModify>table.m_UpdateStore input.m_PointX").val(pointX);
							$("#m_StoreModify>table.m_UpdateStore input.m_PointY").val(pointY);
						});
					});
					if ( pointX != "" && pointY != "" )
						$.StoreList.NaverMapViewr(pointX, pointY, address);
				});
				
				$("#btnImageAdd").click(function() {
					var data = $("#m_StoreModify>table.m_UpdateStore").tagdata();
					if ( $("#m_StoreModify>table.m_UpdateStore td.m_FileList table tr td").size() >= 5 )
					{
						alert("이미지는 5개까지 첨부할수 있습니다." );
						return;
					}
					$.manager.fileopen(7, "$.StoreList.Callback");
				});
			});
		},
		
		MapObj : null,
		markObj : null,
		infoWin : null,
		NaverMapViewr : function(PointX, PointY, address)
		{
			$("#m_MapContainer").show();
			var opts = {width:600, height:400, mapMode:0};
			var point = new NPoint(PointX,PointY);
			if ( $.StoreList.MapObj != null )
			{
				$.StoreList.MapObj.clearOverlays();
				$.StoreList.MapObj.setCenter(point ,2);
			}
			else
			{
				$.StoreList.MapObj = new NMap(document.getElementById('m_MapContainer'),opts);
				$.StoreList.MapObj.setCenterAndZoom(point ,2);
			}
			
			/* 지도 컨트롤 생성 */
			var zoom = new NZoomControl();
			zoom.setAlign("left"); zoom.setValign("top");
			$.StoreList.MapObj.addControl(zoom);

			/* 지도 좌표, 축적 수 준 초기화 */
			var iconURL = "/images/coffine.jpg";
			var iconSize = new NSize(30, 30);
			var icon = new NIcon(iconURL, iconSize)
			
			
			/* 지도 모드 변경 버튼 생성 */
			var mapBtns = new NMapBtns();
			mapBtns.setAlign("right"); mapBtns.setValign("top");
			$.StoreList.MapObj.addControl(mapBtns);
			
			// 아이콘 표시 //
			$.StoreList.markObj = new NMark(point, icon);
			$.StoreList.MapObj.addOverlay( $.StoreList.markObj );
			
			$.StoreList.infowin = new NInfoWindow();
			$.StoreList.MapObj.addOverlay($.StoreList.infowin);
			NEvent.addListener($.StoreList.markObj, "mouseover", function(pos) {
					$.StoreList.infowin.set(pos, '<div style="width:100px; height:50px; background-color:#ffffff; border:solid 1px #666666;">' + address + '</div>');
					$.StoreList.infowin.showWindow()
			});
			NEvent.addListener($.StoreList.markObj, "mouseout", function() {
					$.StoreList.infowin.hideWindow();
			});
		},
		
		Modify : function() {
			var StoreNo = $("#m_StoreModify>table.m_UpdateStore tr:eq(0) td:eq(1)").text();

			var IsActive = $("#m_StoreModify>table.m_UpdateStore select.m_IsActive").val();
			var StoreName = $("#m_StoreModify>table.m_UpdateStore input.m_StoreName").val();

			var CompanyNo1 = $("#m_StoreModify>table.m_UpdateStore input.m_CompanyNo1").val();
			var CompanyNo2 = $("#m_StoreModify>table.m_UpdateStore input.m_CompanyNo2").val();
			var CompanyNo3 = $("#m_StoreModify>table.m_UpdateStore input.m_CompanyNo3").val();
			
			var Phone1 = $("#m_StoreModify>table.m_UpdateStore input.m_Phone1").val();
			var Phone2 = $("#m_StoreModify>table.m_UpdateStore input.m_Phone2").val();
			var Phone3 = $("#m_StoreModify>table.m_UpdateStore input.m_Phone3").val();
			
			var PostNo1 = $("#m_StoreModify>table.m_UpdateStore input.m_PostNo1").val();
			var PostNo2 = $("#m_StoreModify>table.m_UpdateStore input.m_PostNo2").val();
			var Address = $("#m_StoreModify>table.m_UpdateStore input.m_Address").val();
			var CityName = $("#m_StoreModify>table.m_UpdateStore input.m_CityName").val();

			var IsTakeout = $("#m_StoreModify>table.m_UpdateStore select.m_IsTakeout").val();
			var Takeout = $("#m_StoreModify>table.m_UpdateStore input.m_Takeout").val();
			var IsParking = $("#m_StoreModify>table.m_UpdateStore select.m_IsParking").val();
			var Parking = $("#m_StoreModify>table.m_UpdateStore input.m_Parking").val();
			
			var StoreSize = $("#m_StoreModify>table.m_UpdateStore input.m_StoreSize").val();
			var OpenDate = $("#m_StoreModify>table.m_UpdateStore input.m_OpenDate").val();
			var OpenTime = $("#m_StoreModify>table.m_UpdateStore input.m_OpenTime").val();
			
			var PointX = $("#m_StoreModify>table.m_UpdateStore input.m_PointX").val();
			var PointY = $("#m_StoreModify>table.m_UpdateStore input.m_PointY").val();
			
			var Desc = $("#m_StoreModify>table.m_UpdateStore textarea.m_Desc").val();
			
			
			
			if ( IsActive == "Y" )
			{
				if ( $.trim( StoreName ) == "" ) {
					alert( "이름을 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_StoreName").focus();
					return false;
				}
				
				if ( $.trim( Phone1 ) == "" ) {
					alert( "전화번호를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_Phone1").focus();
					return false;
				}
				if ( $.trim( Phone2 ) == "" ) {
					alert( "전화번호를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_Phone2").focus();
					return false;
				}
				if ( $.trim( Phone3 ) == "" ) {
					alert( "전화번호를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_Phone3").focus();
					return false;
				}
				if ( $.trim( PostNo1 ) == "" ) {
					alert( "주소를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_PostNo1").focus();
					return false;
				}
				if ( $.trim( PostNo2 ) == "" ) {
					alert( "주소를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_PostNo2").focus();
					return false;
				}
				if ( $.trim( Address ) == "" ) {
					alert( "주소를 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_Address").focus();
					return false;
				}
				if ( $.trim( CityName ) == "" ) {
					alert( "지역을 입력하세요");
					$("#m_StoreModify>table.m_UpdateStore input.m_CityName").focus();
					return false;
				}
				
				if ( $.trim( PointX ) == "" ) {
					alert( "네이버 지도을 확인하세요");
					return false;
				}
				
				if ( $.trim( PointY ) == "" ) {
					alert( "네이버 지도을 확인하세요");
					return false;
				}
			}
			
			
			var ImageURLS = "";

			$("#m_StoreModify>table.m_UpdateStore td.m_FileList table tr td").each(function() {
				ImageURLS += ImageURLS == "" ? "" : ",";
				ImageURLS += $(this).find("input.m_ImageURL").val();
			});

			$("#m_StoreModify>table.m_UpdateStore").ajaxNet(
			"/manager/store/storemethod.aspx?_vb=ModifyStore",
			{
				"StoreNo": StoreNo
			
			,	"IsActive": "Y"
			,	"StoreName": StoreName
			,	"CompanyNo": CompanyNo1 + "-" + CompanyNo2 + "-" + CompanyNo3
			,	"Phone": Phone1 + "-" + Phone2 + "-" + Phone3
			,	"PostNo": PostNo1 + "-" + PostNo2
			,	"Address": Address
			,	"CityName": CityName
			,	"IsTakeout": IsTakeout
			,	"Takeout": Takeout
			
			,	"IsParking": IsParking
			,	"Parking": Parking
			
			,	"StoreSize": StoreSize
			,	"OpenDate": OpenDate
			
			,	"PointX": PointX
			,	"PointY": PointY
			
			,	"OpenTime": OpenTime
			,	"Description": Desc
			
			}, function(results, context) {
				$("#m_NaverAPI").hide();
				$("#m_StoreModify>table.m_UpdateStore td.m_FileList").ajaxNet(
				"/manager/store/storemethod.aspx?_vb=CreateImage",
				{
					"StoreNo": StoreNo
				,	"ImageURLs": ImageURLS
				}, function(results, context) {
					alert( "변경되었습니다." );
					location.href = location.href;
				});

				//$("#m_divStoreList").show();
				//$("#m_StoreModify").html("");
				//$.StoreList.GetList();
			});
		}
		
	}
	
	$(function() {
		$.StoreList.Init();
	});
})(jQuery)
</script>


</asp:Content>
